package com.singzi.leetcode.N900;
import java.util.Arrays;
public class T0846 {
    public static void main(String[] args) {
        Solution0846 solution0846 = new Solution0846();
        int[] hand = {3,3,3,3,3,3};
        System.out.println(solution0846.isNStraightHand(hand, 2));
    }
}
class Solution0846 {
    public boolean isNStraightHand(int[] hand, int groupSize) {
        int n = hand.length;
        if(n % groupSize != 0) return false;
        boolean[] visit = new boolean[n];
        int count = 1;
        int comNumber = hand[0];
        Arrays.sort(hand);
        for (int i = 0; i < n; i++) {
            for(int indexOfVisit = i;indexOfVisit < n;indexOfVisit++){
                if (!visit[indexOfVisit]) {
                    i = indexOfVisit;
                    comNumber = hand[i];
                    visit[i] = true;
                    count = 1;
                    break;
                }
            }
            for (int j = i+1; j < n; j++) {
                if(count == groupSize) break;
                if(hand[j] > comNumber+1) return false;
                if(hand[j] == comNumber+1&&!visit[j]){
                    visit[j] = true;
                    comNumber++;
                    count++;
                }
            }
            if(count!=groupSize) return false;
        }
        return true;

    }
}